package com.rtmart.springcloudalibaba.user.controller;

import com.rtmart.springcloudalibaba.common.domain.Product;
import com.rtmart.springcloudalibaba.common.domain.User;
import com.rtmart.springcloudalibaba.user.feign.ProductService;
import com.rtmart.springcloudalibaba.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ProductService productService;
//    private RestTemplate restTemplate;
//    @Autowired
//    private DiscoveryClient discoveryClient;

    @GetMapping("/user/{id}")
    public User findOneById(@PathVariable Long id){
        log.info("接收到的id为：{}", id);
//        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("service-product");
//        ServiceInstance serviceInstance = serviceInstances.get(new Random().nextInt(serviceInstances.size()));
//        log.info("{}:{}",  serviceInstance.getHost(), serviceInstance.getPort());
//        String serviceProduct = "service-product";
//        Product product = restTemplate.getForObject("http://service-product/product/1", Product.class);
        Product product = productService.findOneById(id);
        log.info("查询到的Product信息为：{}", product);
        return userService.findOneById(id);
    }
}
